library(tidyverse)
## -- Attaching packages ------------------------------ tidyverse 1.2.1 --
## √ ggplot2 3.2.1       √ purrr   0.3.2  
## √ tibble  2.1.1       √ dplyr   0.8.0.1
## √ tidyr   0.8.3       √ stringr 1.4.0  
## √ readr   1.3.1       √ forcats 0.4.0
## -- Conflicts --------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(ggmap)
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
## 
## Attaching package: 'ggmap'
## The following object is masked from 'package:plotly':
## 
##     wind
library(gganimate)
str(amazon)
## 'data.frame':    6454 obs. of  5 variables:
##  $ year  : int  1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 ...
##  $ state : Factor w/ 23 levels "Acre","Alagoas",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ month : Factor w/ 12 levels "Abril","Agosto",..: 5 5 5 5 5 5 5 5 5 5 ...
##  $ number: num  0 0 0 0 0 10 0 12 4 0 ...
##  $ date  : Factor w/ 20 levels "1998-01-01","1999-01-01",..: 1 2 3 4 5 6 7 8 9 10 ...
summary(amazon)
##       year              state            month          number     
##  Min.   :1998   Rio        : 717   Janeiro  : 541   Min.   :  0.0  
##  1st Qu.:2002   Mato Grosso: 478   Abril    : 540   1st Qu.:  3.0  
##  Median :2007   Paraiba    : 478   Agosto   : 540   Median : 24.0  
##  Mean   :2007   Alagoas    : 240   Fevereiro: 540   Mean   :108.3  
##  3rd Qu.:2012   Acre       : 239   Julho    : 540   3rd Qu.:113.0  
##  Max.   :2017   Amapa      : 239   Junho    : 540   Max.   :998.0  
##                 (Other)    :4063   (Other)  :3213                  
##          date     
##  1998-01-01: 324  
##  1999-01-01: 324  
##  2000-01-01: 324  
##  2001-01-01: 324  
##  2002-01-01: 324  
##  2003-01-01: 324  
##  (Other)   :4510
glimpse(amazon)
## Observations: 6,454
## Variables: 5
## $ year   <int> 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2...
## $ state  <fct> Acre, Acre, Acre, Acre, Acre, Acre, Acre, Acre, Acre, A...
## $ month  <fct> Janeiro, Janeiro, Janeiro, Janeiro, Janeiro, Janeiro, J...
## $ number <dbl> 0, 0, 0, 0, 0, 10, 0, 12, 4, 0, 0, 0, 1, 0, 0, 0, 0, 1,...
## $ date   <fct> 1998-01-01, 1999-01-01, 2000-01-01, 2001-01-01, 2002-01...
print("Visualizing Data")
## [1] "Visualizing Data"

print("Line graph showing the amount of forest fires per year")
## [1] "Line graph showing the amount of forest fires per year"
df.amazon_1 <- amazon %>% group_by(year)%>% 
        summarise(numberperyear = round(sum(number)))%>% 
        ggplot(aes(x = year, y = numberperyear)) + geom_line()
df.amazon_1

print("Scatter Plot showing the occource of forest fires by state and year")
## [1] "Scatter Plot showing the occource of forest fires by state and year"
## Warning: `line.width` does not currently support multiple values.
print("Bar plot showing the total number of forest fires by region")
## [1] "Bar plot showing the total number of forest fires by region"
print("New dataframe with fires sum up by year and state")
## [1] "New dataframe with fires sum up by year and state"
df.amazon <- amazon %>% group_by(year, state) %>% summarise(fires = sum(number))
print("Heatmap of fires by location and year")
## [1] "Heatmap of fires by location and year"
print("An animation showing the amount of fires over the years of each state in brazil")
## [1] "An animation showing the amount of fires over the years of each state in brazil"

print("Creating geodata in order to map fires on to a map of brazil")
## [1] "Creating geodata in order to map fires on to a map of brazil"
states = distinct(amazon, state)
states_df <- as.data.frame(states)
states_df$country <- "Brazil" 
states_df$location <- paste(states_df$state, states_df$country, sep=",")
location_df <- mutate_geocode(states_df, location)
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Acre,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Alagoas,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Amapa,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Amazonas,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Bahia,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Ceara,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Distrito+Federal,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Espirito+Santo,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Goias,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Maranhao,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Mato+Grosso,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Minas+Gerais,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Par%C3%A1,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Paraiba,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Pernambuco,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Piau,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## "Piau,Brazil" not uniquely geocoded, using "piau - state of minas gerais, brazil"
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rio,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Rondonia,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Roraima,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Santa+Catarina,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sao+Paulo,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Sergipe,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
## Source : https://maps.googleapis.com/maps/api/geocode/json?address=Tocantins,Brazil&key=xxx-dQLFtEZ1fy7un_LrqcwiWOt_WJkN3k
# give state boundaries a white border
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
        scope = 'south america',
        projection = list(type = 'equirectangular'),
        showland = TRUE,
        landcolor = toRGB("gray85"),
        subunitwidth = 1,
        countrywidth = 1,
        subunitcolor = toRGB("white"),
        countrycolor = toRGB("white"))
#adding lon $ lat data back to dataframe
df.amazon$lon <- location_df$lon
df.amazon$lat <- location_df$lat
print("Geo plot of Brazil and it's fires by state")
## [1] "Geo plot of Brazil and it's fires by state"
## Warning: `line.width` does not currently support multiple values.